package pro.lepers.portfolio.view.contents.portfolio.thumb {
	import caurina.transitions.Tweener;

	import fr.digitas.flowearth.command.IBatchable;
	import fr.digitas.flowearth.net.BatchLoaderItem;

	import flash.display.Bitmap;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.net.URLRequest;

	/**
	 * @author plepers
	 */
	public class ThumbHolder extends Holder {
		
		public function ThumbHolder ( w : int, h : int ) {
			super( w, h );
		}


		public function load ( req : URLRequest ) : IBatchable {
			if ( _loader ) {
				_loader.removeEventListener( Event.COMPLETE, onLoaded );
				_loader = null;
			}
			
			_loader = new BatchLoaderItem( req );
			_loader.addEventListener( Event.COMPLETE, onLoaded );
			
			return _loader;
		}
		
		
		override public function set width (value : Number) : void {
			super.width = value;
			_refreshBorders();
			if( _picture ) _picture.width = _width;
		}

		override public function set height (value : Number) : void {
			super.height = value;
			_refreshBorders();
			if( _picture ) _picture.height = _height;
		}

		
		override protected function _build () : void {
			super._build( );
			_borderShape = new Shape();
		}
		
		private function onLoaded (event : Event) : void {
			if( _picture )
				removeChild( _picture );
			
			_picture = _loader.loader.content as Bitmap;
			_picture.smoothing = _locked;
			addChild( _picture );
			
			_picture.alpha = 0;
			Tweener.addTween( _picture, { alpha : 1, time : .4 } );
			
			_picture.width = _width;
			_picture.height = _height;
			
			addChild( _borderShape );
			_refreshBorders( );
		}
		
		private function _refreshBorders() : void {
			_borderShape.graphics.clear( );
			_borderShape.graphics.lineStyle( 1, 0xddd4c1 , 1, true, LineScaleMode.NONE );
			_borderShape.graphics.drawRect(0, 0, _width - 1, _height - 1 );
		}


		private var _borderShape : Shape;

		private var _picture : Bitmap;
		
		private var _loader : BatchLoaderItem;

		
		
		override public function set locked (locked : Boolean) : void {
			super.locked = locked;
			if( _picture ) _picture.smoothing = locked;
		}
	}
}
